package main

import (
	"fmt"
	"io/ioutil"
	"os/exec"
	"strings"
)

// uploadCopies 将所有仓库的副本上传到服务器
func uploadCopies(filenames []string) error {
	const winscpCmdFile = "_winscp.script"

	putFiles := make([]string, 0, len(filenames))
	for _, name := range filenames {
		putFiles = append(putFiles, "put "+name)
	}

	//创建 winscp 命令流的文件
	winscpCmd := fmt.Sprintf(`open sftp://%s:%s@%s/ -hostkey=*
cd %s
%s
close
exit
`, APP.BakHostSFTPUsername, APP.BakHostSFTPPwd, APP.BakHost, APP.BakHostWorkdir, strings.Join(putFiles, "\n"))
	if err := ioutil.WriteFile(winscpCmdFile, []byte(winscpCmd), 0644); err != nil {
		return err
	}

	cmd := exec.Command("cmd", "/c", APP.ScpBin+" /ini=nul /script="+winscpCmdFile+" >_winscp.out")
	return cmd.Run()
}
